home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / plug-ins / perl / examples / perlcc < prev    next >
Encoding:
Text File  |  2001-07-21  |  4.9 KB  |  161 lines

  1. #!/usr/app/bin/perl
  2.  
  3. use Gimp ('__','N_');
  4. use Gimp::Feature;
  5.  
  6. $VERSION='0.0';
  7.  
  8. sub check_gtk {
  9.    $gtk = Gimp::Feature::present 'gtk';
  10.  
  11.    if($gtk) {
  12.       # make a relatively extensive check for gtk capabilities
  13.       # this must be done before initializing Gtk in the main program (thus here)
  14.       # imagine!! it might even FLICKER!!!
  15.       unless(open GTK,"-|") {
  16.          close STDERR;
  17.          require Gtk;
  18.          init Gtk;
  19.          my $w = new Gtk::Dialog;
  20.          show_all $w;
  21.          Gtk->idle_add(sub{main_quit Gtk});
  22.          main Gtk;
  23.          print "OK";
  24.          exit;
  25.       }
  26.       unless (<GTK> eq "OK") {
  27.          $gtk=0;
  28.          Gimp::logger(message => 'gtk module present but unusable', function => 'gtktest');
  29.       }
  30.       close GTK;
  31.    }
  32. }
  33.  
  34. sub generate_status {
  35.    my ($log);
  36.    $log="Feature Status\n\n";
  37.    $log.=sprintf "%-12s %-7s %s\n",'Feature','Present','Description';
  38.    for(sort &Gimp::Feature::list) {
  39.       $log.=sprintf "%-12s %-7s %s\n",$_,Gimp::Feature::present($_) ? 'Yes':'No',Gimp::Feature::describe($_);
  40.    }
  41.    $log;
  42. }
  43.  
  44. sub generate_log {
  45.    my ($log);
  46.    $log="Log Entries\n\n";
  47.    $log.=sprintf "%-16s %-5s %s\n", 'File','Fatal', 'Message';
  48.    for (split /\x00/,Gimp->get_data ('gimp-perl-log')) {
  49.       my ($file,$function,$msg,$installed)=split /\x01/;
  50.       @msg = split /\n/,Gimp::wrap_text ($msg.($function ? " ($function)" : ""),55);
  51.       $log.=sprintf "%-16s %-5s %s\n",$file,$installed ? 'Yes':'No',shift(@msg);
  52.       while(@msg) {
  53.          $log.=sprintf "%-16s %-5s %s\n",'','+->',shift(@msg);
  54.       }
  55.    }
  56.    $log;
  57. }
  58.  
  59. sub gtkview_log {
  60.    if ($_[0]) {
  61.       $_[0]->destroy;
  62.       undef $_[0];
  63.    } else {
  64.       my($title,$log)=@_[1,2];
  65.       my($w,$b,$font,$lines);
  66.       $w = new Gtk::Dialog;
  67.       $w->set_title ($title);
  68.  
  69.       $b = new Gtk::Text;
  70.       $b->set_editable(0);
  71.  
  72.       $lines=$log=~y/\n//;
  73.       $lines=25 if $lines>25;
  74.  
  75.       $font = load Gtk::Gdk::Font "9x15bold";
  76.       $font = fontset_load Gtk::Gdk::Font "-*-courier-medium-r-normal--*-120-*-*-*-*-*" unless $font;
  77.       $font = $b->style->font unless $font;
  78.       $w->vbox->add($b);
  79.       $b->realize; # for gtk-1.0
  80.       $b->insert($font,$b->style->fg(-normal),undef,$log);
  81.       $b->set_usize($font->string_width('M')*80,($font->ascent+$font->descent)*($lines+2));
  82.  
  83.       $b = new Gtk::Button "OK";
  84.       $b->can_default(1);
  85.       $b->grab_default;
  86.       $b->signal_connect(clicked => sub { destroy $w; undef $_[0] });
  87.       $w->action_area->add($b);
  88.  
  89.       show_all $w;
  90.       $_[0]=$w;
  91.    }
  92. }
  93.  
  94. # the extension that's called.
  95. sub extension_perl_control_center {
  96.    check_gtk;
  97.    if ($gtk) {
  98.       my($w,$b);
  99.       my($l,$s);
  100.  
  101.       Gimp::gtk_init;
  102.  
  103.       $w = new Gtk::Dialog;
  104.       $w->set_title ('Perl Control Center');
  105.  
  106.       $b = new Gtk::Button "View Perl Feature Status";
  107.       $b->signal_connect(clicked => sub { gtkview_log $s,'Perl Feature Status',generate_status});
  108.       $w->vbox->add($b);
  109.  
  110.       $b = new Gtk::Button "View Perl Error/Warning Log";
  111.       $b->signal_connect(clicked => sub { gtkview_log $l,'Perl Error/Warning Log',generate_log });
  112.       $w->vbox->add($b);
  113.  
  114.       $b = new Gtk::Button "Clear Perl Error/Warning Log";
  115.       $b->signal_connect(clicked => sub { Gimp->set_data('gimp-perl-log',"") });
  116.       $w->vbox->add($b);
  117.  
  118.       $b = new Gtk::Button "OK";
  119.       $b->can_default(1);
  120.       $b->grab_default;
  121.       $b->signal_connect(clicked => sub { main_quit Gtk });
  122.       $w->action_area->add($b);
  123.       $w->signal_connect(destroy => sub { main_quit Gtk });
  124.       show_all $w;
  125.       main Gtk;
  126.    } else {
  127.       my $temp="/tmp/gimp-perl-$$-".rand; # this is not very secure
  128.       require Fcntl;
  129.       sysopen TEMP,$temp,&Fcntl::O_EXCL|&Fcntl::O_CREAT|&Fcntl::O_WRONLY or die "unable to create temporary file $temp\n";
  130.       print TEMP generate_status,"\n",generate_log,"\n<using xterm for display, press enter to continue>";
  131.       close TEMP;
  132.  
  133.       system("xterm +ls -sb -sl 500 -geometry 80x30 -T 'Perl Control Center Error Log (Version $VERSION)' ".
  134.              "-e sh -c 'cat $temp; rm -f $temp; read' >/dev/null 2>&1");
  135.  
  136.       if ($? >> 8 && -f $temp) {
  137.          system("xterm -e sh -c 'cat $temp; rm -f $temp; read' >/dev/null 2>&1");
  138.       }
  139.       if ($? >> 8) {
  140.          print STDERR "\n",generate_status,"\n",generate_log,"\n";
  141.          Gimp->message (generate_status."\n".generate_log."\n<using gimp_message for display>");
  142.       }
  143.       unlink $temp;
  144.    }
  145. }
  146.  
  147. Gimp::on_run {
  148.    extension_perl_control_center;
  149. };
  150.  
  151. Gimp::on_query {
  152.   Gimp->install_procedure("extension_perl_control_center", "the perl control center gives information about gimp-perl",
  153.                           "The perl control center gives information about the status of gimp-perl and allows configuration of important system parameters",
  154.                           "Marc Lehmann", "Marc Lehmann", $VERSION,
  155.                           N_"<Toolbox>/Xtns/Perl/Control Center...", undef, &Gimp::EXTENSION,
  156.                           [[&Gimp::PDB_INT32, "run_mode", "Interactive, [non-interactive]"]], []);
  157. };
  158.  
  159. exit Gimp::main;
  160.  
  161.